package no.gnet.s10.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import no.gnet.util.Constants;
import no.gnet.util.S10Props;

/**
 * Servlet implementation class for Servlet: Img
 *
 * @web.servlet
 *   name="Img"
 *   display-name="Img" 
 *
 * @web.servlet-mapping
 *   url-pattern="/Img"
 *  
 */
 public class Img extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public Img() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = S10Props.getInstance().getProps().getProperty(Constants.IMAGES_HOME);
		String filen = request.getParameter("in");
		File imgfile = new File(path+"/"+filen);
		response.setContentType("image/jpeg");
		response.setDateHeader("Expires",
				   System.currentTimeMillis(  ) + 24*60*60*1000);
		System.out.println("Streaming file "+imgfile.getAbsolutePath());	
		InputStream in = new FileInputStream(imgfile);
		int len=0;
		byte[] bytes = new byte[4096];
		while((len=in.read(bytes))>0){
			response.getOutputStream().write(bytes, 0, len);
		}
		response.getOutputStream().close();
//		String [] files = dir.list();
//		for (int i = 0; i < files.length; i++) {
//			
//		}
	}  	  	  	    
}